package com.frkj.yuan.api.presenter;

import com.frkj.yuan.api.bean.OrderMsgBean;
import com.frkj.yuan.api.bean.OrderPayorderMsgBean;
import com.frkj.yuan.api.bean.PayOrderNoBean;
import com.frkj.yuan.api.category.OrderMsgCategory;
import com.frkj.yuan.base.BaseBean;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.RetrofitUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/4/11 17 ：01
 * 项目名称：yuanban
 */
public class OrderListPresenter implements OrderMsgCategory.Presenter{
    OrderMsgCategory.View view;
    public OrderListPresenter(OrderMsgCategory.View view) {
        this.view = view;
    }
    @Override
    public void getOrderList(int pageNumber, String mToken ,String requestBody) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestBody);
        RetrofitUtil.getInstance().getmService().getOrderList(mToken,pageNumber, ConstantStaticUrl.PAGE_NUMBER,body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    OrderMsgBean userInfor = gson.fromJson(result, OrderMsgBean.class);
                    view.getOrderList(userInfor);
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }

    @Override
    public void getPayOrderMsg(String orderNo, String mToken, long userId) {
        RetrofitUtil.getInstance().getmService().getPayOrderMsg(mToken,userId, orderNo).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    OrderPayorderMsgBean userInfor = gson.fromJson(result, OrderPayorderMsgBean.class);
                    view.getPayOrderMsg(userInfor);
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }

    @Override
    public void getCancle(String mToken, String requestBody) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestBody);
        RetrofitUtil.getInstance().getmService().get_cancel_daizhifu(mToken,body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    BaseBean userInfor = gson.fromJson(result, BaseBean.class);
                    view.getCancle(userInfor);
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }

    @Override
    public void getTuikuanMoney(String orderNo, String mToken, long userId) {
        RetrofitUtil.getInstance().getmService().getTuikuanMoney(mToken,userId,orderNo).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    BaseBean userInfor = gson.fromJson(result, BaseBean.class);
                    view.getTuikuanMoney(userInfor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }

}
